package master;

/**
 * @author chenyw
 * @date 2022/9/3 22:53
 */
public class M209minSubArrayLen {
    public static void main(String[] args) {
        int i = minSubArrayLen(11, new int[]{
                1, 2, 3, 4, 5
        });
        System.out.println(i);
    }
    public static int minSubArrayLen(int target, int[] nums) {
        int i = 0;
        int j = 0;
        int sum = nums[0];
        int min = Integer.MAX_VALUE;
        while (j < nums.length && i < nums.length) {
            if (sum == target) {
                min = Math.min(min, j - i + 1);
                j++;
                if (j < nums.length) {
                    sum += nums[j];
                }
            } else if (sum > target) {
                min = Math.min(min, j - i + 1);
                sum -= nums[i];
                i++;
            } else {

                j++;
                if (j < nums.length) {
                    sum += nums[j];
                }
            }
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }
}
